package com.web.work;

import com.web.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/four")
public class Test4 extends HttpServlet {
    //4.传入学生信息，实现学生插入

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置内容类型
        resp.setContentType("application/json;charset=utf-8");
        String sql="insert into student(name,age,gender,idcard,address) values(?,?,?,?,?)";
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String idcard = req.getParameter("idcard");
        String address = req.getParameter("address");
        int i = JDBCUtils.update(sql, name, age, gender, idcard, address);
        if(i>0){
            resp.getWriter().write("{\"msg\":\"添加成功\"}");
        }else{
            resp.getWriter().write("{\"msg\":\"添加失败\"}");
        }


    }
}
